package com.wisezone.frame.service.impl;

import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpSession;

import com.wisezone.common.OnlineUserMgr;
import com.wisezone.common.bean.bo.UserInfo;
import com.wisezone.common.exception.WzServiceException;
import com.wisezone.frame.service.FrLoginService;
import com.wisezone.permission.bean.entity.WzDepartmentEntity;
import com.wisezone.permission.bean.entity.WzGroupEntity;
import com.wisezone.permission.bean.entity.WzPersonEntity;
import com.wisezone.permission.bean.entity.WzPositionEntity;
import com.wisezone.permission.bean.entity.WzRoleEntity;
import com.wisezone.permission.bean.entity.WzUserEntity;
import com.wisezone.permission.service.WzDepartmentService;
import com.wisezone.permission.service.WzGroupService;
import com.wisezone.permission.service.WzPersonService;
import com.wisezone.permission.service.WzPositionService;
import com.wisezone.permission.service.WzRoleService;

/**
 * 登录服务类 2010-09-30
 * 
 * @author shan
 * 
 */
public class FrLoginServiceImpl implements FrLoginService {

	private WzPersonService wzPersonService;

	private WzRoleService wzRoleService;

	private WzPositionService wzPositionService;

	private WzDepartmentService wzDepartmentService;

	private WzGroupService wzGroupService;

	/**
	 * 用户注入
	 * 
	 * @param sessionId
	 * @param userEntity
	 * @throws WzServiceException
	 */
	public void userInject(String sessionId, WzUserEntity userEntity)
			throws WzServiceException {

		Long userId = userEntity.getId();
		UserInfo user = new UserInfo();
		user.setId(userId);

		if (userEntity.getPersonId() != null) {
			WzPersonEntity entity = wzPersonService.findById(userEntity
					.getPersonId());
			user.setUserName(entity.getName());

			if (entity.getPositionId() != null) {
				WzPositionEntity positionEntity = wzPositionService
						.findById(entity.getPositionId());
				user.setPositionId(positionEntity.getId());
				user.setPositionName(positionEntity.getName());
			}
			if (entity.getDepartmentId() != null) {
				WzDepartmentEntity deptEntity = wzDepartmentService
						.findById(entity.getDepartmentId());
				user.setDeptId(deptEntity.getId());
				user.setDeptName(deptEntity.getName());
			}
		}

		List<WzRoleEntity> roleList = wzRoleService.listRoleByUserId(userId);
		if (roleList != null && roleList.size() > 0) {
			String roleIds = "";
			String roleNames = "";
			for (Iterator<WzRoleEntity> it = roleList.iterator(); it.hasNext(); it
					.hasNext()) {
				WzRoleEntity roleEntity = it.next();
				roleIds += "," + roleEntity.getId();
				roleNames += "," + roleEntity.getName();
			}
			if (!roleIds.equals("")) {
				user.setRoleIds(roleIds.substring(1));
			}
			if (!roleNames.equals("")) {
				user.setRoleNames(roleNames.substring(1));
			}
		}

		List<WzGroupEntity> groupList = wzGroupService
				.listGroupByUserId(userId);
		if (groupList != null && groupList.size() > 0) {
			String groupNames = "";
			for (Iterator<WzGroupEntity> it = groupList.iterator(); it
					.hasNext(); it.hasNext()) {
				WzGroupEntity groupEntity = it.next();
				groupNames += "," + groupEntity.getName();
			}
			if (!groupNames.equals("")) {
				user.setGroupNames(groupNames.substring(1));
			}
		}

		// 用户存储处理
		OnlineUserMgr.inject(sessionId, user);
	}

	/**
	 * 注销
	 * 
	 * @param session
	 * @throws WzServiceException
	 */
	public void logout(HttpSession session) throws WzServiceException {

		if (session == null) {
			throw new IllegalArgumentException("session 不能为空");
		}
		OnlineUserMgr.destory(session.getId());
		session.invalidate();
		session = null;
	}

	public void setWzPersonService(WzPersonService wzPersonService) {
		this.wzPersonService = wzPersonService;
	}

	public void setWzRoleService(WzRoleService wzRoleService) {
		this.wzRoleService = wzRoleService;
	}

	public void setWzDepartmentService(WzDepartmentService wzDepartmentService) {
		this.wzDepartmentService = wzDepartmentService;
	}

	public void setWzGroupService(WzGroupService wzGroupService) {
		this.wzGroupService = wzGroupService;
	}

	public void setWzPositionService(WzPositionService wzPositionService) {
		this.wzPositionService = wzPositionService;
	}
	

}
